package com.gxa.controller;

import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.param.ContractParam;
import com.gxa.param.TableParam;
import com.gxa.pojo.Contract;
import com.gxa.pojo.Empl;
import com.gxa.service.ContractService;
import com.gxa.service.EmplService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    private ContractService service;

    /**
     * 合同创建列表页面
     * @return
     */
    @GetMapping("/create/page")
    public String list(){
        return "contract/contract-list";
    }


    /**
     * 合同数据
     * @return
     */
    @PostMapping("/list/data")
    @ResponseBody
    public LayR  listData(ContractParam param){
        return service.list(param);
    }


    /**
     * 合同的添加页面
     * @return
     */

    @GetMapping("/add/page")
    public String addPage(){
        return "contract/contract-add";
    }

    @GetMapping("/edit/page")
    public String editPage(@Param("id") Integer id,HttpSession session)
    {
        session.setAttribute("contract",service.findById(id));
         return "contract/contract-edit";
    }

    @GetMapping("/audit/list")
    public String auditList()
    {
        return "contract/audit-list";
    }

    @GetMapping("/audit/page")
    public String auditPage(@Param("id") Integer id,HttpSession session)
    {
        session.setAttribute("contract",service.findById(id));
        return "contract/audit-edit";
    }


    /**
     * 添加合同
     * @param contract
     * @return
     */
    @PostMapping("/add/do")
    @ResponseBody
    public LayR add(Contract contract, HttpSession session){
        return service.add(contract,(String) session.getAttribute("emplName"));
    }

    @PostMapping("/edit/do")
    @ResponseBody
    public LayR edit(Contract contract, HttpSession session){
        return service.edit(contract,(String) session.getAttribute("emplName"));
    }

    @PostMapping("/sub/do")
    @ResponseBody
    public LayR submit(@RequestParam("id") Integer id){
        return service.submit(id);
    }

    @PostMapping("/audit/do")
    @ResponseBody
    public LayR audit(HttpSession session,@RequestParam("id") Integer id,@RequestParam("statusId")Integer statusId,@Param("advice")String advice){
        System.out.println("hhhh===="+advice);
        return service.audit(id,statusId,advice,(String) session.getAttribute("emplName"));
    }

    @PostMapping("/findEmplByName")
    @ResponseBody
    public LayR findEmplByName(String emplName){
        return service.findEmplByName(emplName);
    }

    @PostMapping("/category/data")
    @ResponseBody
    public LayR findAllCategory(){
        return service.findAllCategory();
    }

    @PostMapping("/status/data")
    @ResponseBody
    public LayR findAllStatus(){
        return service.findAllStatus();
    }




    @PostMapping("/submit/contract")
    @ResponseBody
    public R submit(Contract contract){
        return null;
    }

    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestParam("ids[]") String[] ids){
        return service.deleteAll(ids);
    }

}
